//
//  SLRootViewModel.m
//  Objective-C
//
//  Created by S_Line on 2018/5/3.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLRootViewModel.h"

@interface SLRootViewModel()

@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) NSMutableDictionary *dataSource;

@end

@implementation SLRootViewModel

- (instancetype)init {
    
    self = [super init];
    if (self) {
        [self initData];
    }
    
    return self;
}

- (void)initData {
    self.sections = [NSMutableArray new];
    self.dataSource = [NSMutableDictionary new];
    
    NSMutableArray *dataArray = [NSMutableArray new];
    
    NSString *firstSectionName = @"SLBaseViewController基础信息";
    [self.sections addObject:firstSectionName];
    
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"navBar" routeK:@"g_base_nav" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"runtime" routeK:@"g_demo_runtime" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"kvc" routeK:@"g_demo_kvc" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"kvo" routeK:@"g_demo_kvo" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Core Animation" routeK:@"g_demo_ca" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"TransitionAnimation" routeK:@"g_demo_transition_learn" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Timer" routeK:@"g_demo_timer" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Store" routeK:@"g_demo_store" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"UICollectionView" routeK:@"g_demo_collectionview" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Test" routeK:@"g_demo_test" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Download" routeK:@"g_demo_download" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"NestedScrollView" routeK:@"g_demo_nestedscrollview" routeV:nil]];
    
    [self.dataSource setObject:[dataArray copy] forKey:firstSectionName];
    
    
    NSString *secondSectionName = @"第三方库示范";
    [self.sections addObject:secondSectionName];
    [dataArray removeAllObjects];
    
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"MBP" routeK:@"g_demo_mbp" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"POP" routeK:@"g_demo_pop" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"MJRefresh" routeK:@"g_demo_mjrefresh" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"ReactiveObjC" routeK:@"g_demo_reactive_programming" routeV:nil]];
    
    [self.dataSource setObject:[dataArray copy] forKey:secondSectionName];
    
    
    NSString *thirdSectionName = @"EffectiveObjectiveC";
    [self.sections addObject:thirdSectionName];
    [dataArray removeAllObjects];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"MessageFrowarding" routeK:@"g_demo_eoc_message_forwarding" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"MethodSwizzling" routeK:@"g_demo_eoc_method_swizzling" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"AssociatedObject" routeK:@"g_demo_eoc_associated_object" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Description" routeK:@"g_demo_eoc_description" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Block" routeK:@"g_demo_eoc_block" routeV:nil]];
    [self.dataSource setObject:[dataArray copy] forKey:thirdSectionName];
    
    
    NSString *forthSectionName = @"Interview";
    [self.sections addObject:forthSectionName];
    [dataArray removeAllObjects];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"CircularReference" routeK:@"g_demo_interview_circular_reference" routeV:nil]];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Animation" routeK:@"g_demo_interview_animation" routeV:nil]];
    [self.dataSource setObject:[dataArray copy] forKey:forthSectionName];
    
    NSString *fifthSectionName = @"Algorithm";
    [self.sections addObject:fifthSectionName];
    [dataArray removeAllObjects];
    [dataArray addObject:[SLRootModelItem routeItemWithName:@"Recursion" routeK:@"g_demo_recursion" routeV:nil]];
    [self.dataSource setObject:[dataArray copy] forKey:fifthSectionName];
}

- (NSInteger)numberOfSections {
    return self.sections.count;
}

- (NSString *)titleForSection:(NSInteger)section {
    NSString *title = @"";
    
    if (self.sections[section] && [self.sections[section] isKindOfClass:[NSString class]]) {
        title = self.sections[section];
    }
    
    return title;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionData = self.dataSource[self.sections[section]];
    
    NSInteger numberOfRows = 0;
    
    if ([sectionData isKindOfClass:[NSArray class]]) {
        numberOfRows = sectionData.count;
    }
    
    return numberOfRows;
}

- (id)objectForIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionKey = self.sections[indexPath.section];
    NSArray *dataArray = self.dataSource[sectionKey];
    return dataArray[indexPath.row];
}

@end
